﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Yunwei.BizLogic;
using Yunwei.Domain.Models;
using Yunwei.IBizLogic;

namespace Yunwei.Web.Areas.RRShop.Models
{
    public class RestaurantIngredientViewModel
    {
        public int Id { get; set; }

        [Required(ErrorMessage = "必填")]
        [StringLength(20, MinimumLength = 1, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "名称")]
        public string Name { get; set; }

        [Required(ErrorMessage = "必填")]
        [Display(Name = "进价")]
        public decimal Cost { get; set; }

        [Required(ErrorMessage = "必填")]
        [Display(Name = "存货")]
        public decimal Stock { get; set; }

        [Required(ErrorMessage = "必填")]
        [StringLength(10, MinimumLength = 1, ErrorMessage = "{2}到{1}个字符")]
        [Display(Name = "单位")]
        public string Unit { get; set; }

        [Display(Name="供货商")]
        public int Vendor_Id { get; set; }

        public int Restaurant_Id { get; set; }

        public int Ingredient_Id { get; set; }

        [NotMapped]
        public bool IsFood { get; set; }
        [NotMapped]
        public decimal Amount { get; set; }

        private List<string> units;
        public List<string> Units
        {
            get
            {
                if (units == null)
                {
                    units = new List<string>();
                    units.Add("份");
                    units.Add("克");
                    units.Add("两");
                    units.Add("斤");
                    units.Add("公斤");
                    units.Add("吨");
                    units.Add("块");
                    units.Add("罐");
                    units.Add("瓶");
                    units.Add("袋");
                    units.Add("把");
                    units.Add("个");
                    units.Add("台");
                    units.Add("箱");
                }

                return units;
            }
        }

        private List<SelectListItem> vItems;
        public List<SelectListItem> VendorItems
        {
            get
            {
                if (vItems == null)
                {
                    IRestaurantVendorService vendorService = new RestaurantVendorService();
                    var vrs = vendorService.FindList(100, this.Restaurant_Id, 1);
                    vItems = new List<SelectListItem>();
                    List<RestaurantVendorViewModel> rvms = new List<RestaurantVendorViewModel>();
                    foreach (var vr in vrs)
                    {
                        RestaurantVendorViewModel rvm=RestaurantVendorViewModel.FromRestaurantVendor(vr);
                        rvms.Add(rvm);
                    }

                    foreach(var rv in rvms)
                    {
                        vItems.Add(new SelectListItem()
                        {
                            Value = rv.Vendor_Id.ToString(),
                            Text = rv.Vendor.Name,
                            Selected = rv.Vendor_Id == this.Vendor_Id
                        });
                    }

                }

                return vItems;
            }
        }

        private List<SelectListItem> items;
        public List<SelectListItem> UnitItems
        {
            get
            {
                if (items == null)
                {
                    items = new List<SelectListItem>();
                    foreach (string unit in Units)
                    {
                        SelectListItem item = new SelectListItem()
                        {
                            Text = unit,
                            Value = unit
                        };

                        if (this.Unit == unit)
                            item.Selected = true;

                        items.Add(item);
                    }
                }
                return items;
            }
        }

        private RestaurantIngredient _restaurantIngredient;
        public RestaurantIngredient RestaurantIngredient
        {
            get
            {
                if (_restaurantIngredient == null)
                {
                    _restaurantIngredient = new RestaurantIngredient()
                    {
                        Id = this.Id,
                        Cost = this.Cost,
                        Stock = this.Stock,
                        Name = this.Name,
                        Unit = this.Unit,
                        Vendor_Id = this.Vendor_Id,
                        Restaurant_Id = this.Restaurant_Id,
                        Ingredient_Id = this.Ingredient_Id
                    };
                }

                return _restaurantIngredient;
            }
        }

        public static RestaurantIngredientViewModel FromRestaurantIngredient(RestaurantIngredient ri)
        {
            RestaurantIngredientViewModel rivm = new RestaurantIngredientViewModel()
            {
                Id = ri.Id,
                Cost = ri.Cost,
                Stock = ri.Stock,
                Name = ri.Name,
                Unit = ri.Unit,
                Vendor_Id = ri.Vendor_Id,
                Restaurant_Id = ri.Restaurant_Id,
                Ingredient_Id = ri.Ingredient_Id
            };

            return rivm;
        }
    }
}